package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Employee;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.security.DigestException;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpServletRequest request){
            String username = employee.getUsername();
            String password = employee.getPassword();
            //密码加密
            password = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<Employee> wrapper= new QueryWrapper<>();
        wrapper.eq("username",username);
        Employee emp = employeeService.getOne(wrapper);
        if (emp== null){
            return R.error("用户不存在");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if (emp.getStatus()==0){
            return R.error("用户被禁用");
        }
        //将走到此处的id存入session会话
        request.getSession().setAttribute("emp",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("emp");
            return R.success("退出成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        QueryWrapper<Employee> wrapper=new QueryWrapper<>();
        if (name!=null){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("update_time");
        Page<Employee> page1 =new Page<>(page,pageSize);
        employeeService.page(page1,wrapper);
        return R.success(page1);
    }
    @PostMapping
    public R<String> addEmp(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工信息=====》"+employee.toString());
        //设置初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        boolean save = employeeService.save(employee);
        return save?R.success("成功"):R.error("失败");
    }
    //修改员工状态
    @PutMapping
    public R<String> banEmp(HttpServletRequest request,@RequestBody Employee employee){

        boolean b = employeeService.updateById(employee);
        return b?R.success("成功"):R.error("失败");
    }
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable("id") Long id){
        return R.success(employeeService.getById(id));
    }

}
